package com.heping.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heping.common.Result;
import com.heping.entity.Product;
import com.heping.mapper.ProductMapper;
import com.heping.service.ProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @PostMapping("/productForPage")
    public Result<IPage<Product>> queryForPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                                                @RequestBody Product product) {
        IPage<Product> pageParam = new Page<>(pageNo, pageSize);
        return Result.success(productService.queryForPage(pageParam,product));
    }

    @PostMapping("addProduct")
    public Result<String> addProduct(@RequestBody Product product) {
        productService.save(product);
        return Result.success("新增成功");
    }

    @PostMapping("/updateProduct")
    public Result<String> updateProduct(@RequestBody Product product) {
        productService.updateById(product);
        return Result.success("更新成功");
    }

    @GetMapping("/getProductDetails")
    public Result<Product> getProductDetails(@RequestParam Long id) {
        Product product = productService.getById(id);
        return Result.success(product);
    }

    @GetMapping("/delProduct")
    public Result<String> delProduct(@RequestParam Long id) {
        productService.removeById(id);
        return Result.success("删除成功");
    }


}
